package NG::Sys::App::Add;
use strict;
use warnings;
use utf8;

sub template_tt {
	my ($self) = @_;
	unless($self->{template_tt}){
		require NG::Sys::TT;
		$self->{template_tt} = NG::Sys::TT->new($self);
	}
	return $self->{template_tt};
}
sub make_syslog {
	my ($self,$tag) = @_;
	require NG::Sys::Log;
	return NG::Sys::Log::get_logger($tag);
}
sub syslog {
	my $self = shift;
	unless($self->{_syslog}){
		my $tag = $self->{syslog} || $self->{cfg}{syslog} || die "need syslog";
		$self->{_syslog} = $self->make_syslog($tag);
	}
	$self->{_syslog}->send(@_);
}
sub logger {
	my ($self,$another) = @_;
	unless($self->{_logger}){
		my $tag = $self->{logger} || $self->{cfg}{logger} || die "need _logger";
		require NG::Sys::LogFile;
		NG::Sys::LogFile::connect($self->{cfg}{path}{log});
		$self->{_logger} = NG::Sys::LogFile::get_logger($tag);
	}
	if($another){
		return NG::Sys::LogFile::get_logger($another);
	}
	return $self->{_logger};
}
sub debug {
	my ($self) = @_;
	my $sub;
	unless($sub = $self->{_debug}){
		my $tag = $self->{debug} || $self->{cfg}{debug};
		if($tag && $tag eq "syslog"){
			$sub = $self->{_debug} = \&syslog;
		}else{
			if($tag){
				$self->{logger} = $tag;
			}
			$sub = $self->{_debug} = sub {
				my $self = shift;
				$self->logger->debug(@_);
			};
		}
	}
	$sub->(@_);
}
sub dbh {
	my ($self) = @_;
	unless($self->{dbh}){
		require NG::Sys::DB;
		my $srv;
		if(my $id = $self->{cfg}{db_local}){
			$srv = $self->{cfg}{db}{$id};
			$srv->{local} = $srv->{sock} ? 1 : 0;
			$srv->{id} = $id;
		}else{
			die "no db_local\n";
		}
		$self->{dbh} = NG::Sys::DB::get_dbh($srv);
	}
	return $self->{dbh};
}
sub dbh_to {
	my ($self,$id,$noauto) = @_;
	if(my $srv = $self->{cfg}{db}{$id}){
		$srv->{id} = $id;
		require NG::Sys::DB;
		return NG::Sys::DB::get_dbh($srv,$noauto);
	}else{
		die "no db for '$id'\n";
	}
}
sub warn_to_logger {
	my ($self) = @_;
	$self->ext_warn(
		sub {
			NG::Sys::App::app->logger->warning(
					join "\n", map {s/\=HASH\(\S+?\)//g;$_} grep {$_!~/eval/} map {split /\r?\n/} Carp::longmess(@_)
				);
		});
}
sub die_to_logger {
	my ($self) = @_;
	$self->ext_die(
		sub{
			NG::Sys::App::app->logger->error(
					join "\n", map {s/\=HASH\(\S+?\)//g;$_} grep {$_!~/eval/} map {split /\r?\n/} Carp::longmess(@_)
				);
			Carp::confess(@_);
		});
}
sub get_cache {
	my ($self,$type,$args) = @_;
	require NG::Sys::Cache;
	return NG::Sys::Cache->new($type,$args);
}
sub show_counters{
	my ($self) = @_;
	my $x = $self->get_context("counter");
	my @list;
	while(my ($k,$v) = each %$x){
		my $str = $k.":";
		while(my ($n,$c) = each %$v){
			$str .= " ".$n."=".$c;
		}
		push @list,$str;
	}
	return @list;
}
sub local_block {
	my $self = shift;
	unless($self->{_local_block}){
		require NG::Sys::Spec::BlockerClient;
		$self->{_local_block} = new NG::Sys::Spec::BlockerClient($self,"local_block");
	}
	return $self->{_local_block};
}
sub global_block {
	my $self = shift;
	unless($self->{_global_block}){
		require NG::Sys::Spec::BlockerClient;
		$self->{_global_block} = new NG::Sys::Spec::BlockerClient($self,"global_block");
	}
	return $self->{_global_block};
}
1;
